package transform.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * Represet the configuration of system.
 * 
 * @author jarocena
 *
 */
public class TransformConfiguration {

	private List<Cost> costs;
	private String fileName;
	


	public TransformConfiguration() {
		costs = new Vector<Cost>();
		fileName = "";
	}
	
	public static TransformConfiguration newDefautConfiguration(){
		TransformConfiguration conf = new TransformConfiguration();
		conf.addCost(new Cost(1));
		conf.addCost(new Cost(3));
		conf.addCost(new Cost(5));
		return conf;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public void addCost(Cost c){
		costs.add(c);
	}
	
	
	public Iterator<Cost> getCost(){
		return costs.iterator();
	}
	
	//fatory Method
	public static TransformConfiguration load(String file) throws FileNotFoundException{
		XStream xs = new XStream(new DomDriver());
		FileInputStream fis = new FileInputStream(file);
		TransformConfiguration conf = (TransformConfiguration) xs.fromXML(fis);
		
		return conf;
	}
	
	public void save() throws IOException{

		XStream xs = new XStream(new DomDriver());

		//Write to a file in the file system
		FileOutputStream fs = new FileOutputStream(this.getFileName());
		xs.toXML(this, fs);
		fs.close();
	}
	
	public static TransformConfiguration loadFromXML(String xml){
		XStream xs = new XStream(new DomDriver());
		return (TransformConfiguration)xs.fromXML(xml);
	}
	
	public String toXML(){
		XStream xs = new XStream(new DomDriver());
		String s = xs.toXML(this);
//		System.out.println(s);
		return s;
	}
	
	public List<Cost> getCosts(){
		return this.costs ;
	}
	
	public static void main(String[] args) {
		
		TransformConfiguration t = TransformConfiguration.newDefautConfiguration();
		System.out.println(t.toXML());
		
//		System.out.println("Start");
//		TransformConfiguration conf = new TransformConfiguration();
//		conf.setFileName("D:\\test.at");
//		Cost c = new Cost(5);
//		conf.addCost(c);
//		try {
//			conf.save();
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("done!");

		//TODO ESTO FUNCIONA pasar a codigo.
		
//		System.out.println("start");
//		TransformConfiguration conf = new TransformConfiguration();
//		conf.setFileName("D:\\esteEsElarchivo.txt");
//		conf.addCost(new Cost(5));
//		conf.addCost(new Cost(6));
//		XStream xs = new XStream(new DomDriver());
//
//	        //Write to a file in the file system
//	        try {
//	            FileOutputStream fs = new FileOutputStream("D:\\test05.xml");
//	            xs.toXML(conf, fs);
//	        } catch (FileNotFoundException e1) {
//	            e1.printStackTrace();
//	        }
//	        
//	       System.out.println("done");
		
		System.out.println("start");
	
		
		
	       
	       System.out.println("done");
		
	}

	public boolean isFileSetted() {
		return !this.fileName.equals("");
	}
}
